package com.xzz.graph;

import java.util.HashMap;

/**
 * @author: hhz
 * @create: 2022-02-18 09:29
 * 1791. 找出星型图的中心节点
 **/
public class FindCenter {
    public static void main(String[] args) {
        System.out.println(findCenter(new int[][]{{1, 2}, {2, 3}, {4, 2}}));

        System.out.println(findCenter(new int[][]{{1, 2}, {5, 1}, {1, 3}, {1, 4}}));

        System.out.println(findCenter(new int[][]{{1, 3}, {2, 3}}));
    }

    public static int findCenter(int[][] edges) {
        int a = edges[0][0];
        int b = edges[0][1];
        if (edges[1][0] == a || edges[1][1] == a) {
            return a;
        } else {
            return b;
        }
    }

    public static int findCenter2(int[][] edges) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < edges.length; i++) {
            for (int j = 0; j < 2; j++) {
                int num = edges[i][j];
                map.put(num, map.getOrDefault(num, 0) + 1);
                if (map.get(num) == edges.length) {
                    return num;
                }
            }
        }
        return -1;
    }
}
